<?php
/**
 * Created by IntelliJ IDEA.
 * User: Kmontyjack
 * Date: 9.11.11
 * Time: 12:31
 * To change this template use File | Settings | File Templates.
 * Zend Search Lucene
 */

class Knihovna_Model_SearchKniha
{
    /**
     * @var Zend_Search_Lucene_Interface
     */
    protected $index, $analyzer, $id_kniha;



    public function create_index_kniha()
    {

        $this->index = Zend_Search_Lucene::create('../application/var/search/kniha');

        $this->analyzer = new Zend_Search_Lucene_Analysis_Analyzer_Common_Utf8_CaseInsensitive();

    }

    public function __construct()
    {
    }

    /**
     * Vezme knihu a oindexuje si složky, podle kterých poté vyhledává
     * @param $data_kniha array ve kterém je uložen název knihy, klíčová slova
     *
     * @return void
     */
    public function pridejKnihu($data_kniha)
    {

        $this->index = Zend_Search_Lucene::open('../application/var/search/kniha');

        $kniha = new Zend_Search_Lucene_Document();

        $kniha->addField(Zend_Search_Lucene_Field::Text('nazev', $data_kniha['nazev'], 'UTF-8'));

        $kniha->addField(Zend_Search_Lucene_Field::Text('klicova_slova', $data_kniha['klicova_slova'], 'UTF-8'));

        $kniha->addField(Zend_Search_Lucene_Field::Text('id_kniha', $data_kniha["rid"]));

        $this->index->addDocument($kniha);

        $this->index->commit();

        $this->id_kniha++;

    }

     /**
     * Po delší době je dobré optimalizovat indexy
     *
     * @return void
     */
    Public function Optimalizuj_knihu()
    {
        $this->index = Zend_Search_Lucene::open('../application/var/search/kniha');

        $this->index->optimize();
    }

     /**
     *
     * Vyhledává knihu podle názvu a klíčových slov
     * @param $dotaz_kniha
     *
     * @return $hits
     */

    public function Hledej_knihu($dotaz_kniha)
    {
        $this->index = Zend_Search_Lucene::open('../application/var/search/kniha');

        $this->query = new Zend_Search_Lucene_Search_Query_MultiTerm();

        $this->query->addTerm(new Zend_Search_Lucene_Index_Term($dotaz_kniha, 'nazev'));

        $this->query->addTerm(new Zend_Search_Lucene_Index_Term($dotaz_kniha, 'klicova slova'));

        $hits = $this->index->find($this->query);                                //hits je to, co našel

        return $hits;
    }

      /**
    public function Smaz_knihu()
    {
        $this->index = Zend_Search_Lucene::open('../application/var/search/kniha');

        Zend_Search_Lucene_Analysis_Analyzer::setDefault(
            new Zend_Search_Lucene_Analysis_Analyzer_Common_Utf8_CaseInsensitive()
            );

        $hits = $this->index->find('');
        foreach ($hits as $hit)
        {
            $this->index->delete($hit->id)
        }

    }
      */

}